﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.IO;
using System.Text;

namespace sharpCmd.Classes
{
    public class FSObjectInfo
    {
        static FSObjectInfo()
        {
            SizeFormatInfo = new NumberFormatInfo();
            SizeFormatInfo.NumberGroupSeparator = ",";
            SizeFormatInfo.NumberGroupSizes = new int[] { 3 };
            SizeFormatInfo.NumberDecimalDigits = 0;
            DateTimeFormatInfo = new DateTimeFormatInfo();
        }

        public const int DIRECTORY_INITIAL_SIZE = -1;
        public static NumberFormatInfo SizeFormatInfo { get; set; }
        public static DateTimeFormatInfo DateTimeFormatInfo { get; set; }

        public FSObjectType Type;
        public string Name;
        public string Extension;
        public string FullName;
        public long Size;
        public DateTime LastWriteTime;
        public FileAttributes Attributes;

        /// <summary>
        /// If this item is a fake entry, e.g. ".." directory
        /// </summary>
        public bool Fake;

        public bool AlwaysOnTop;

        public void SetSize(long size)
        {
            this.Size = size;
        }

        public string ToFullString()
        {
            return Type == FSObjectType.Directory ? "[" + Name + "]" : Name + "." + Extension;
        }

        public override string ToString()
        {
            return Name;
        }

        public string DisplayString
        {
            get { return Type == FSObjectType.Directory ? "[" + Name + "]" : Name; }
        }

        public string SizeString
        {
            get { return Size >= 0 ? string.Format(SizeFormatInfo, "{0:N}k", Size / 1024) : string.Empty; }
        }

        public string AttributeString
        { //rahs[ce]
            get
            {
                return string.Format("{0}{1}{2}{3}",
                (Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly ? 'r' : '-',
                (Attributes & FileAttributes.Hidden) == FileAttributes.Hidden ? 'h' : '-',
                (Attributes & FileAttributes.System) == FileAttributes.System ? 's' : '-',
                (Attributes & FileAttributes.Compressed) == FileAttributes.Compressed ? 'c' :
                (Attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted ? 'e' : '-');
            }
        }

        string[] strings;
        public string[] GetFullInfos()
        {
            const int FULLINFOS_COUNT = 5;
            if (strings == null)
                strings = new string[FULLINFOS_COUNT];
            strings[0] = Name;
            strings[1] = Extension;
            strings[2] = SizeString;
            strings[3] = LastWriteTime.ToString("u");
            strings[4] = AttributeString;
            return strings;
        }
    }

    public enum FSObjectType
    {
        File, Directory
    }
}